{% load static %}

<!DOCTYPE html>
<html>
<head>

    <meta charset="UTF-8">
    <title>Title</title>
    {% include 'admin/includes/css-part.html' %}
    <style>
        textarea {
            color: whitesmoke !important;;
            background-color: #1f2c39 !important;
            font-size: 16px;
            font-family: 'Heiti SC';
            line-height: 24px;
            border: 1px solid #eee;
            height: 800px;
            min-height: 780px;
            max-height: 800px;
            width: 100%;
            word-spacing: 3px;
            letter-spacing: 2px;
            padding: 5px;
            overflow-y: scroll;
            word-break: break-all;
        }
    </style>
</head>
<body>
<div id="app">
    <div style="width: 100%">
        <el-radio-group v-model="name" @input="get_config_settings" size="small">
            <el-radio-button label="ptools.toml"></el-radio-button>
            <el-radio-button label="hosts"></el-radio-button>
        </el-radio-group>
        <el-popconfirm title="确定修改配置文件吗？" @confirm="saveSettings">
            <el-button type="primary" slot="reference" size="small">保存</el-button>
        </el-popconfirm>
    </div>

    <div style="width: 100%;margin-top: 5px;">
        <el-input v-model="settings" type="textarea" autosize="{minRows: 30}" autocorrect="off"></el-input>
    </div>

</div>
{% include 'admin/includes/js-part.html' %}
<script src="{% static 'admin/simpleui-x/js/axios.min.js' %}"></script>
<script>
    const vm = new Vue({
        el: '#app',
        data() {
            return {
                settings: '',
                name: 'ptools.toml',
            }
        },
        beforeMount() {
            this.get_config_settings()
        },
        methods: {
            get_config_settings() {
                axios.get(
                    "{% url 'get_config_api' %}" + '?name=' + this.name
                ).then(res => {
                    console.log('获取数据列表成功', res)
                    {#let data = res.data#}
                    {#console.log(typeof res.data.data)#}
                    if (res.data.code === 0) {
                        this.settings = res.data.data
                        console.log(this.settings)
                        //this.$message({
                        //    type: 'success',
                        //    message: res.data.msg
                        //});
                    } else {
                        this.loading = false
                        this.$message({
                            type: 'warning',
                            message: '获取数据列表失败！'
                        });
                    }
                }).catch(res => {
                    console.log('获取数据列表失败', res)
                    this.$message({
                        type: 'warning',
                        message: '获取数据列表失败！' + res
                    });
                })
            },
            saveSettings() {
                axios({
                    method: "post",
                    headers: {
                        "content-type": "application/json",
                    },
                    url: "{% url 'save_config_api' %}",
                    data: {
                        name: this.name,
                        settings: this.settings
                    }
                }).then(res => {
                    console.log(res)
                    {#let data = res.data#}
                    {#console.log(typeof res.data.data)#}
                    if (res.data.code === 0) {
                        console.log(res.data.msg)
                        this.get_config_settings()
                        this.$message({
                            type: 'success',
                            message: res.data.msg
                        });
                    } else {
                        this.loading = false
                        this.$message({
                            type: 'warning',
                            message: res.data.msg
                        });
                    }
                }).catch(res => {
                    console.log('保存失败', res)
                    this.$message({
                        type: 'warning',
                        message: '数据保存失败！' + res
                    });
                })
            }
        }
    })
</script>
</body>
</html>